package com.learn.educommentboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.eduapi.entity.ResponseDTO;
import com.learn.eduapi.entity.comment.CourseComment;
import com.learn.eduapi.entity.comment.CourseCommentFavoriteRecord;
import com.learn.educommentboot.service.CourseCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程留言
 */
@RestController
@CrossOrigin
@RequestMapping("comment")
public class adminCommentController {
    @Autowired
    private CourseCommentService courseCommentService;

    /**
     * 获取留言列表
     * @param find
     * @param page
     * @param size
     * @return
     */
    @GetMapping("getCommentList")
    public ResponseDTO<List<CourseComment>> getCommentList(@RequestParam("find")String find,
                                                           @RequestParam("page")Integer page,
                                                           @RequestParam("size")Integer size){
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        List<CourseComment> commentList = courseCommentService.getCommentList(find, (page - 1) * size, size);
        for (int i = 0; i < commentList.size(); i++) {
            if (commentList.get(i).getIsTop()==true){
                commentList.get(i).setIsTopId(1);
            }else {
                commentList.get(i).setIsTopId(0);
            }
            if (commentList.get(i).getIsReply()==true){
                commentList.get(i).setIsReplyId(1);
            }else {
                commentList.get(i).setIsReplyId(0);
            }
            if(commentList.get(i).getIsDel()==true){
                commentList.get(i).setIsDelId(1);
            }else {
                commentList.get(i).setIsDelId(0);
            }
            if (commentList.get(i).getIsNotify()==true){
                commentList.get(i).setIsNotifyId(1);
            }else {
                commentList.get(i).setIsNotifyId(0);
            }
            if (commentList.get(i).getReplied()==true){
                commentList.get(i).setRepliedId(1);
            }else{
                commentList.get(i).setRepliedId(0);
            }
        }
        responseDTO.setContent(commentList);
        responseDTO.setState(courseCommentService.getCommentList(find,0,0).size());
        responseDTO.setMessage("获取留言列表成功");
        return responseDTO;
    }

    /**
     * 置顶留言
     * @param id
     * @param isTopId
     * @return
     */
    @GetMapping("loveComment/{id}/{isTopId}")
    public ResponseDTO<CourseComment> loveComment(@PathVariable("id")Integer id,
                                                  @PathVariable("isTopId")Integer isTopId){
        ResponseDTO<CourseComment> responseDTO = new ResponseDTO<>();
        QueryWrapper<CourseComment> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        CourseComment courseComment = new CourseComment();
        if (isTopId==1){
            courseComment.setIsTop(true);
        }else {
            courseComment.setIsTop(false);
        }
        int i = courseCommentService.updateAdminComment(courseComment,wrapper);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
        }
        return responseDTO;
    }

    /**
     * 删除留言
     * @param id
     * @return
     */
    @GetMapping("delAdminComment/{id}")
    public ResponseDTO<CourseComment> delComment(@PathVariable("id")Integer id){
        System.out.println("id = " + id);
        ResponseDTO<CourseComment> responseDTO = new ResponseDTO<>();
        int i = courseCommentService.delComment(id);
        System.out.println("i = " + i);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
        }
        return responseDTO;
    }

    /**
     * 审核留言
     * @param userId
     * @param status
     * @return
     */
    @GetMapping("saveAdminComment/{id}/{status}")
    public ResponseDTO<CourseComment> saveAdminComment(@PathVariable("id")Integer userId,
                                                       @PathVariable("status")Integer status){
        ResponseDTO<CourseComment> responseDTO = new ResponseDTO<>();
        QueryWrapper<CourseComment> wrapper = new QueryWrapper<>();
        wrapper.eq("id",userId);
        CourseComment courseComment = new CourseComment();
        courseComment.setStatus(status);
        int i = courseCommentService.updateAdminComment(courseComment,wrapper);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
        }
        return responseDTO;
    }

    /**
     * 留言详情追踪列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("getAdminCommentRecord")
    public ResponseDTO<List<CourseComment>> getAdminCommentRecord(@RequestParam("find")String find,
                                                                  @RequestParam("page")Integer page,
                                                            @RequestParam("size")Integer size){
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(courseCommentService.getAdminCommentRecord(find,(page-1)*size,size));
        responseDTO.setState(courseCommentService.getAdminCommentRecord(find,0,0).size());
        return responseDTO;
    }

    @GetMapping("getCrouseRavoritesUserList/{id}")
    public ResponseDTO<List<CourseCommentFavoriteRecord>> getCrouseRavoritesUserList(@PathVariable("id")Integer id){

        ResponseDTO<List<CourseCommentFavoriteRecord>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(courseCommentService.getCrouseRavoritesUserList(id));
        responseDTO.setState(200);
        return responseDTO;
    }

    @GetMapping("removeCommentRavorites/{id}")
    public ResponseDTO<CourseCommentFavoriteRecord> removeCommentRavorites(@PathVariable("id")Integer id){

        ResponseDTO<CourseCommentFavoriteRecord> responseDTO = new ResponseDTO<>();
        int i = courseCommentService.delAmdinCommentFavorite(id);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
        }
        return responseDTO;
    }
}
